package cn.sh.basic.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;

public class EmailUtils {
    @Autowired
    private JavaMailSender javaMailSender;


    public void sendEmail() throws Exception{
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1589867195@qq.com");
        helper.setSubject("邮件标题");
        //第二个参数 true 代表是否是一个html内容
        helper.setText("<h1>新型冠状病毒防护守则</h1>"+
                "<img src='https://img0.baidu.com/it/u=349104223,4266572771&fm=253&fmt=auto&app=138&f=JPEG?w=240&h=320\"' />",true);
        //添加附件
        helper.addAttachment("文件名.jpg",new File("E:\\BaiduNetdiskDownload\\学习资料\\4.jpg"));
        //收件人
        helper.setTo("1589867195@qq.com");
        //发送邮件
        javaMailSender.send(mimeMessage);
    }
}
